package com.twu.biblioteca;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created with IntelliJ IDEA.
 * User: ruchita
 * Date: 20/9/13
 * Time: 8:36 AM
 * To change this template use File | Settings | File Templates.
 */
public class ReserveBookFromList {
    public JPanel panel1;
    private JList BookList;
    private JButton reserveBookButton;


    public ReserveBookFromList() {
        DefaultListModel dlm = new DefaultListModel();
        //Getting list books from sample database  and adding to Jlist
        BooksSampleDatabase s = new BooksSampleDatabase();
        s.createDatabase();
        for (String BookName : s.bookInfo.keySet())
            dlm.addElement(BookName);
        BookList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        BookList.setModel(dlm);

        reserveBookButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                BookList.getSelectedValue();
                System.out.print(BookList.getSelectedValue());
                String messageString = "";
                //Check for availibility of book
                ReserveBook rb = new ReserveBook();
                Boolean isexists = rb.reserveSpecifiedBook((String) BookList.getSelectedValue());
                if (isexists == true)
                    messageString = "Thank You! Enjoy the book.";

                else
                    messageString = "Sorry we don't have that book yet";
                JOptionPane.showMessageDialog(panel1, messageString);

            }
        });
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        panel1 = new JPanel();
        panel1.setLayout(new BorderLayout(0, 0));
        final JPanel panel2 = new JPanel();
        panel2.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
        panel1.add(panel2, BorderLayout.NORTH);
        reserveBookButton = new JButton();
        reserveBookButton.setText("Reserve Book");
        panel2.add(reserveBookButton);
        final JPanel panel3 = new JPanel();
        panel3.setLayout(new BorderLayout(0, 0));
        panel1.add(panel3, BorderLayout.CENTER);
        BookList = new JList();
        panel3.add(BookList, BorderLayout.CENTER);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return panel1;
    }
}
